import { createSlice } from '@reduxjs/toolkit'

const initialState = [
    {id:1, name: '张三', type: '成人', code: '12022220000000022', status: true},
    {id:2, name: '李四', type: '未成年', code: '12022220000000022', status: true},
    {id:3, name: '王五', type: '成人', code: '12022220000000022', status: true},
    {id:4, name: '赵六', type: '老年人', code: '12022220000000022', status: true},
]

const personSlice = createSlice({
  name: 'person',
  initialState,
  reducers: {
    removeSelectedPerson(state, {payload}) {
        const index = state.findIndex( item => item.id === payload )
        state[index].status = false;
    },
    addSelectedPerson(state, {payload}) {
        const index = state.findIndex( item => item.id === payload )
        state[index].status = true;
    },
    delPerson(state, {payload}) {
      const index = state.findIndex( item => item.id === payload )
      state.splice(index, 1)
    },
    addPerson(state, {payload}) {
      const nData = {...payload, id:Math.random(), status: true}
      state.push( nData );
    },
    updatePerson(state, {payload}) {
      const index = state.findIndex( item => item.id === payload.id )
      state[index].name = payload.name
      state[index].type = payload.type
      state[index].code = payload.code
    }
  },
})

export const {updatePerson, delPerson, removeSelectedPerson, addSelectedPerson, addPerson } = personSlice.actions

export default personSlice.reducer